package top.lishuoboy.cloud.provider.payment8001.controller;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import top.lishuoboy.cloud.api.common.entry.CommonResult;
import top.lishuoboy.cloud.api.common.entry.Payment;
import top.lishuoboy.cloud.provider.payment8001.mapper.PaymentMapper;
import top.lishuoboy.cloud.provider.payment8001.service.PaymentService;

/**
 *
 */
@Slf4j
@RestController
public class PaymentController {
    @Autowired
    private PaymentMapper paymentMapper;
    @Value("${server.port}")
    private int serverPort;
    @Autowired
    PaymentService paymentService;

    /**
     * http://localhost:8001/provider/payment/create
     * body 为 serial=104
     */
    @PostMapping("provider/payment/create")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentMapper.insert(payment);
        log.info("***插入结果 result=={}", result);

        if (result > 0) {
            return new CommonResult(200, "插入数据库成功, serverPort: " + serverPort, result);
        } else {
            return new CommonResult(444, "插入数据库失败", null);
        }
    }

    /** http://localhost:8001/provider/payment/get?serial=1 */
    @SneakyThrows
    @GetMapping("provider/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
//        TimeUnit.SECONDS.sleep(3); // 测试 openfeign的ribbon超时时间

        Payment payment = paymentMapper.selectById(id);

        if (payment != null) {
            return new CommonResult(200, "查询成功,serverPort: " + serverPort, payment);
        } else {
            return new CommonResult(444, "没有对应记录,查询ID: " + id, null);
        }
    }

    /** 非高并发响应秒回的业务 */
    @GetMapping("provider/payment/hystrix/ok")
    private String paymentInfoOK() {
        return "OK,     线程: " + Thread.currentThread().getName();
    }

    @GetMapping("provider/payment/hystrix/timeout")
    private String paymentInfoTimeOut() {
        return paymentService.paymentInfoTimeOut();
    }

    /**
     * 服务熔断
     * 默认在10秒滚动窗口内，每当20个请求中，有50%失败时，熔断器就会打开5秒。此时再调用此服务，将会直接返回失败，不再调远程服务。直到5s钟之后，重新检测该触发条件，判断是否把熔断器关闭，或者继续打开
     * http://localhost:8001/payment/circuit/1
     * http://localhost:8001/payment/circuit/-1
     * 多次错误（-1），再来次正确，但错误得显示
     * 重点测试 - 多次错误，然后慢慢正确，发现刚开始不满足条件，就算是正确的访问地址也不能进行
     */
    @GetMapping("/payment/circuit/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        String result = paymentService.paymentCircuitBreaker(id);
        log.info("result: " + result);
        return result;
    }

    @GetMapping("/provider/payment/lb")
    public String getPaymentLB() {
        return serverPort + "";
    }


    @GetMapping("/provider/payment/zipkin")
    public String paymentZipkin() {
        return "hi ,i'am paymentzipkin server fall back，welcome to here, O(∩_∩)O哈哈~";
    }
}
